package com.sxrong.carloan.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.sxrong.carloan.R;
import com.sxrong.carloan.adapter.CarAdapter;
import com.sxrong.carloan.dto.CarInfoBean;
import com.sxrong.carloan.dto.CarInfoModel;
import com.sxrong.carloan.net.ApiService;
import com.sxrong.carloan.net.MyCallBack;
import com.sxrong.carloan.net.XUtils;
import com.sxrong.carloan.utils.CommonListener;

import java.util.HashMap;

public class SearchActivity extends Activity {
    private static final int EDIT_REQUEST_CODE = 101;

    private CarAdapter adapter;
    private EditText et_search;
    private ImageView iv_delete;
    private TextView btn_search;
    private ImageView iv_back;
    private RecyclerView recycleView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        iv_back = findViewById(R.id.iv_back);
        et_search = findViewById(R.id.et_search_goods);
        iv_delete = findViewById(R.id.iv_delete);
        btn_search = findViewById(R.id.tv_search);
        recycleView = findViewById(R.id.recyclerView);
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new CarAdapter(this);
        recycleView.setAdapter(adapter);
        adapter.setCommonListener(new CommonListener<CarInfoBean>() {
            @Override
            public void onItemClick(int pos, CarInfoBean data) {
                Intent intent = new Intent(SearchActivity.this, CarInfoActivity.class);
                intent.putExtra("data" , data);
                startActivityForResult(intent , EDIT_REQUEST_CODE);
            }
        });

        initAction();

    }

    private void initAction() {
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        et_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                iv_delete.setVisibility(TextUtils.isEmpty(et_search.getText().toString().trim()) ? View.GONE : View.VISIBLE  );
            }
        });

        iv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                et_search.setText("");
            }
        });

        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(TextUtils.isEmpty(et_search.getText().toString().trim())){
                    Toast.makeText(SearchActivity.this, "请填写内容", Toast.LENGTH_SHORT).show();
                    return;
                }
                sendSearch();
            }
        });
    }

    private void sendSearch() {
        HashMap<String , String> map  = new HashMap<>();
        map.put("text" , et_search.getText().toString().trim());
        XUtils.Get(ApiService.SEARCH_CAR_INGO_URL , map , new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                if(isFinishing()){
                    return;
                }
                CarInfoModel model = new Gson().fromJson(result, CarInfoModel.class);
                if(model.getCode() != 1){
                    Toast.makeText(SearchActivity.this, model.getMsg(), Toast.LENGTH_SHORT).show();
                    return;
                }
                //Toast.makeText(SearchActivity.this, model.getMsg(), Toast.LENGTH_SHORT).show();
                if(adapter == null){
                    return;
                }
                adapter.setList(model.getData());
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                if(isFinishing()){
                    return;
                }
                Toast.makeText(SearchActivity.this, "服务器错误", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == EDIT_REQUEST_CODE){
            sendSearch();
        }
    }


}
